<?php

/**
 * Sigleton class:Application
 *
 */
require_once("./time.func.php");
class Application
{
	static $instance;
	private $startTime = 0;//record when the application is started
	private $registeredObj = array();
	private $objNum = 0;
	static function getInstance()
	{
		if(is_object(self::$instance))
		{
			return self::$instance;
		}
		else
		{
			self::$instance = new Application();
			return self::$instance;
		}
	}
	public function registerObj($obj)
	{
		if(is_object($obj))
		{
			$this->registeredObj[$obj->getId()] = &$obj;
			$this->objNum ++;
			return true;
		}
		else
		{
			return false;
		}
	}
	public function releaseObj($obj)
	{
		if(is_object($obj) && array_key_exists($obj->getId(),$this->registeredObj))
		{
			unset($this->registeredObj[$obj->getId()]);
			$this->objNum --;
		}
		else
		{
			return false;
		}
	}
	public function listObj()
	{
		var_dump($this->registeredObj);
	}
	public function getStartTime()
	{
		return $this->startTime;
	}
	public function getRunTime()
	{
		return microtime_float() - $this->startTime;
	}
	public function getObjNum()
	{
		return $this->objNum;
	}
	private function __construct()
	{
		$this->startTime = microtime_float();
	}
	private function __clone()
	{
	}
}

/**
 * abstract class:Object
 *
 */
abstract class Object
{
	protected $id = 0;//the unique identity of the object
	private static $idCreator = 0;
	public function __construct()
	{
		self::$idCreator ++;
		$this->id = self::$idCreator;
		$application = Application::getInstance();
		if(!$application->registerObj($this))
		{
			echo "obj register failed!";
		}
	}

	public function _destroy($var)
	{
		Application::getInstance()->releaseObj($this);
		return NULL;
	}
	public function getId()
	{
		return $this->id;
	}
}

?>